import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // 前台路由
    {
      path: '/',
      redirect: '/index'
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/Login.vue'),
      meta: {
        title: '登录',
        requiresAuth: false
      }
    },
    {
      path: '/register',
      name: 'Register',
      component: () => import('../views/Register.vue'),
      meta: {
        title: '注册',
        requiresAuth: false
      }
    },
    {
      path: '/index',
      name: 'Index',
      component: () => import('../views/Index.vue'),
      meta: {
        title: '首页',
        requiresAuth: true
      }
    },
    {
      path: '/publishproduct',
      name: 'PublishProduct',
      component: () => import('../views/PublishProduct.vue'),
      meta: {
        title: '发布商品',
        requiresAuth: true
      }
    },
    {
      path: '/personalcenter',
      name: 'PersonalCenter',
      component: () => import('../views/PersonalCenter.vue'),
      meta: {
        title: '个人中心',
        requiresAuth: true
      }
    },
    {
      path: '/jiaoyi',
      name: 'Jiaoyi',
      component: () => import('../views/Jiaoyi.vue'),
      meta: {
        title: '交易市场',
        requiresAuth: true
      }
    },
    {
      path: '/myproduct',
      name: 'MyProduct',
      component: () => import('../views/MyProduct.vue'),
      meta: {
        title: '我的商品',
        requiresAuth: true
      }
    },
    {
      path: '/xiaoxi',
      name: 'Xiaoxi',
      component: () => import('../views/Xiaoxi.vue'),
      meta: {
        title: '消息中心',
        requiresAuth: true
      }
    },
    {
      path: '/product/:id',
      name: 'ProductDetail',
      component: () => import('@/views/ProductDetail.vue'),
      props: true,
      meta: {
        title: '商品详情页',
        requiresAuth: true
      }
    },

    // 后台路由
    {
      path: '/admin',
      redirect: '/admin/dashboard'
    },
    {
      path: '/admin/dashboard',
      name: 'AdminDashboard',
      component: () => import('../views/admin/AdminDashboard.vue'),
      meta: {
        title: '控制面板',
        requiresAuth: true,
        role: 'admin'
      }
    },
    {
      path: '/admin/products',
      name: 'AdminProducts',
      component: () => import('../views/admin/AdminProducts.vue'),
      meta: {
        title: '商品管理',
        requiresAuth: true,
        role: 'admin'
      }
    },
    {
      path: '/admin/users',
      name: 'AdminUsers',
      component: () => import('../views/admin/AdminUsers.vue'),
      meta: {
        title: '用户管理',
        requiresAuth: true,
        role: 'admin'
      }
    },
    {
      path: '/admin/orders',
      name: 'AdminOrders',
      component: () => import('../views/admin/AdminOrders.vue'),
      meta: {
        title: '订单管理',
        requiresAuth: true,
        role: 'admin'
      }
    },
    {
      path: '/admin/messages',
      name: 'AdminMessages',
      component: () => import('../views/admin/AdminMessages.vue'),
      meta: {
        title: '消息管理',
        requiresAuth: true,
        role: 'admin'
      }
    }
  ]
})

router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.role === 'admin') {
    document.title = to.meta.title ? `${to.meta.title} - 后台管理` : '后台管理'
  } else {
    document.title = to.meta.title || '二手交易平台'
  }
  
  // 检查是否需要认证
  if (to.meta.requiresAuth === false) {
    return next()
  }

  // 获取用户信息
  const user = JSON.parse(localStorage.getItem('user') || {})
  const token = localStorage.getItem('token')

  // 后台路由检查
  if (to.meta.role === 'admin') {
    if (token && user.role === 'admin') {
      return next()
    } else {
      ElMessage.warning("请先以管理员身份登录")
      return next({ path: "/login" }) // 重定向到主登录页
    }
  }
  
  // 前台路由检查
  if (token) {
    next()
  } else {
    ElMessage.warning("请先登录")
    next({ path: "/login" })
  }
})

export default router